package com.ynu.springbootappdemo.service;

import com.ynu.springbootappdemo.common.exception.BusinessException;
import com.ynu.springbootappdemo.common.factory.UserBeanFactory;
import com.ynu.springbootappdemo.common.service.LogicService;
import com.ynu.springbootappdemo.common.utils.ErrorCode;
import com.ynu.springbootappdemo.common.utils.TokenUtils;
import com.ynu.springbootappdemo.dao.IUserDao;
import com.ynu.springbootappdemo.model.dto.User.RegisterDTO;
import com.ynu.springbootappdemo.model.entity.User;
import com.ynu.springbootappdemo.model.vo.TokenVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

import static com.ynu.springbootappdemo.common.constant.CommonConstant.ACCESS_TOKEN;
import static com.ynu.springbootappdemo.common.constant.CommonConstant.REFRESH_TOKEN;

@Service
public class UserService extends LogicService<IUserDao, User,Long> {
    public UserService(@Autowired IUserDao dao) {
        super(dao);
    }
    @Resource
    private UserBeanFactory factory;
    @Resource
    private TokenUtils tokenUtils;

    /**
     *  TODO
     * @param userPhone
     * @param password
     * @return
     */
    public TokenVo login(String userPhone, String password){
        if (StringUtils.isNoneBlank(userPhone)&&StringUtils.isNoneBlank(password)){
            User user = getDAO().findUserByUserPhoneAndPassword(userPhone,password);
            if (user == null) {
                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"账号或者密码错误");
            }
            //生成AccessToken
            String token = TokenUtils.createToken(user,ACCESS_TOKEN);
            //生成refreshToken
            String refreshToken = TokenUtils.createToken(user,REFRESH_TOKEN);
            //最后进行缓存
            tokenUtils.saveTokenToRedis(user,token,refreshToken);
            return new TokenVo(token,refreshToken);
        }
        throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"账号或者密码不能为空");
    }

    @Resource
    private UploadService uploadService;
    public User register(RegisterDTO user){
        //判断用户是否已存在
        User userDB = getDAO().findUserByUserPhone(user.getUserPhone());
        if (Objects.isNull(userDB)){
            //上传头像
            String avatarUrl = uploadService.uploadFile(user,"userAvatar");
            user.setUserAvatar(avatarUrl);
            try {
                //抽象反射工厂创建实例
                User instance = factory.getInstance(user.getUserRole());
                BeanUtils.copyProperties(user,instance);
                return dao.save(instance);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    /**
     * 通过token获取当前用户
     * */
    public User getLoginUser(HttpServletRequest request){
        return tokenUtils.getCurrentUser(request,ACCESS_TOKEN);
    }

}
